var _excluded = ["playScale", "replay", "component", "always", "scrollEvent", "appear", "location", "targetId", "onChange", "onScroll", "componentProps"];
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? Object(arguments[r]) : {}, o = Object.keys(t); "function" == typeof Object.getOwnPropertySymbols && o.push.apply(o, Object.getOwnPropertySymbols(t).filter(function (e) { return Object.getOwnPropertyDescriptor(t, e).enumerable; })), o.forEach(function (r) { _defineProperty(e, r, t[r]); }); } return e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropGet(t, e, o, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), e, o); return 2 & r && "function" == typeof p ? function (t) { return p.apply(o, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { createElement } from 'react';
import PropTypes from 'prop-types';
import EventListener from './EventDispatcher';
import ScrollElement from './ScrollElement';
import { toArrayChildren, noop, windowIsUndefined } from './util';
var ScrollOverPack = /*#__PURE__*/function (_ScrollElement) {
  function ScrollOverPack(props) {
    var _this;
    _classCallCheck(this, ScrollOverPack);
    _this = _callSuper(this, ScrollOverPack, [props]);
    _defineProperty(_this, "checkContentHeight", function () {
      var bodyHeight = document.body.scrollHeight;
      var windowHeight = window.innerHeight;
      if (bodyHeight <= windowHeight) {
        _this.setState({
          show: true
        });
      }
    });
    _defineProperty(_this, "scrollEventListener", function (e) {
      _this.getParam(e);
      var show = _this.state.show;
      var _this$props = _this.props,
        always = _this$props.always,
        replay = _this$props.replay;
      var isTop = _this.elementShowHeight > _this.clientHeight + _this.leavePlayHeight;
      if (_this.enter || !replay && isTop) {
        if (!show) {
          _this.setState({
            show: true
          });
        }
        if (!always && _this.eventType) {
          EventListener.removeEventListener(_this.eventType, _this.scrollEventListener, _this.target);
        }
      } else if (always) {
        var bottomLeave = _this.elementShowHeight < _this.playHeight;
        // 设置往上时的出场点...
        var topLeave = replay ? isTop : null;
        if (topLeave || bottomLeave) {
          if (show) {
            _this.setState({
              show: false
            });
          }
        }
      }
    });
    _this.children = toArrayChildren(props.children);
    _this.oneEnter = false;
    _this.enter = false;
    _this.state = {
      show: false,
      children: toArrayChildren(props.children)
    };
    return _this;
  }
  _inherits(ScrollOverPack, _ScrollElement);
  return _createClass(ScrollOverPack, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this2 = this;
      if (prevProps !== this.props) {
        var always = this.props.always;
        var show = this.state.show;
        var inListener = EventListener._listeners.scroll && EventListener._listeners.scroll.some(function (c) {
          return c.n === _this2.eventType.split('.')[1];
        });
        if (always && !inListener) {
          this.addScrollEvent();
        } else if (!always && !show) {
          this.scrollEventListener();
        }
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      if (windowIsUndefined) {
        return;
      }
      this.dom = ReactDOM.findDOMNode(this);
      var date = Date.now();
      this.target = this.props.targetId && document.getElementById(this.props.targetId);
      var length = EventListener._listeners.scroll ? EventListener._listeners.scroll.length : 0;
      this.eventType = "scroll.scrollEvent".concat(date).concat(length);
      this.addScrollEvent();
      this.pollingInterval = setInterval(this.checkContentHeight, 300);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.pollingInterval) {
        clearInterval(this.pollingInterval);
      }
      // 调用父类的 componentWillUnmount 方法
      _superPropGet(ScrollOverPack, "componentWillUnmount", this, 3)([]);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        playScale = _this$props2.playScale,
        replay = _this$props2.replay,
        component = _this$props2.component,
        always = _this$props2.always,
        scrollEvent = _this$props2.scrollEvent,
        appear = _this$props2.appear,
        location = _this$props2.location,
        targetId = _this$props2.targetId,
        onChange = _this$props2.onChange,
        onScroll = _this$props2.onScroll,
        componentProps = _this$props2.componentProps,
        props = _objectWithoutProperties(_this$props2, _excluded);
      if (windowIsUndefined) {
        return /*#__PURE__*/createElement(component, _objectSpread({}, props, componentProps), props.children);
      }
      var childToRender;
      if (!this.oneEnter) {
        var show = !appear;
        var children = toArrayChildren(props.children).map(function (item) {
          if (!item) {
            return null;
          }
          var key = item.key || (Date.now() + Math.random()).toString(16).replace('.', '');
          return item.type.isTweenOne ? /*#__PURE__*/React.cloneElement(item, _objectSpread({}, item.props, {
            key: key,
            paused: !show
          })) : /*#__PURE__*/React.cloneElement(item, _objectSpread({}, item.props, {
            key: key
          }), show && item.props.children);
        });
        childToRender = /*#__PURE__*/createElement(component, _objectSpread({}, props, componentProps), children);
        this.oneEnter = true;
      } else {
        if (!this.state.show) {
          this.children = this.children.map(function (item) {
            if (!item) {
              return null;
            }
            var key = item.key || (Date.now() + Math.random()).toString(16).replace('.', '');
            // 判断 TweenOne;
            if (item.type.isTweenOne) {
              return /*#__PURE__*/React.cloneElement(item, {
                key: key,
                reverse: true
              });
            }
            return /*#__PURE__*/React.cloneElement(item, {
              key: key
            }, null);
          });
        } else {
          this.children = this.state.children;
        }
        childToRender = /*#__PURE__*/createElement(component, _objectSpread({}, props, componentProps), this.children);
      }
      return childToRender;
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps;
      var nextState = {
        prevProps: props
      };
      if (prevProps && props !== prevProps) {
        nextState.children = toArrayChildren(props.children);
      }
      return nextState;
    }
  }]);
}(ScrollElement);
_defineProperty(ScrollOverPack, "propTypes", {
  component: PropTypes.any,
  playScale: PropTypes.any,
  always: PropTypes.bool,
  scrollEvent: PropTypes.func,
  children: PropTypes.any,
  className: PropTypes.string,
  style: PropTypes.any,
  replay: PropTypes.bool,
  onChange: PropTypes.func,
  onScroll: PropTypes.func,
  appear: PropTypes.bool,
  componentProps: PropTypes.object
});
_defineProperty(ScrollOverPack, "defaultProps", {
  component: 'div',
  playScale: 0.5,
  always: true,
  scrollEvent: noop,
  replay: false,
  onChange: noop,
  onScroll: noop,
  appear: true,
  componentProps: {}
});
ScrollOverPack.isScrollOverPack = true;
export default ScrollOverPack;